package me.keiwu.admin.controller;

import me.keiwu.admin.domain.user.Label;
import me.keiwu.admin.service.LabelService;
import me.keiwu.admin.vo.LabelSearch;
import me.keiwu.pub.data.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 *  标签管理
 */
@RequestMapping("/api/label")
@Controller
public class LabelController {

    private static final Logger logger = LoggerFactory.getLogger(LabelController.class);

    @Autowired
    LabelService labelService;

    //多条件分页查询
    @ResponseBody
    @RequestMapping("/list")
    public Response list(LabelSearch labelSearch, HttpServletRequest request, HttpServletResponse response) {
        labelSearch.setPage(labelSearch.getPage());
        Page<Label> pageResult =  labelService.list(labelSearch);

        return Response.ok(pageResult);
    }

    @ResponseBody
    @RequestMapping(value = "/add")
    public Response add(String labelList, HttpServletRequest request, HttpServletResponse response){
        List<Label> resultLabelList = new ArrayList<Label>();

        List<String> labelNameList = java.util.Arrays.asList(labelList.split(","));
        for(String labelName : labelNameList){
            Label label = new Label();
            label.setLabelName(labelName);
            long id = labelService.add(label);
            label.setId(id);

            resultLabelList.add(label);
        }

        return Response.ok(resultLabelList);
    }

    @ResponseBody
    @RequestMapping(value = "/update")
    public Response update(Label label, HttpServletRequest request, HttpServletResponse response){

        labelService.update(label);

        return Response.ok();
    }

    @ResponseBody
    @RequestMapping(value = "/detail")
    public Response detail(Long id){

        Label label = labelService.detail(id);

        return Response.ok(label);
    }

    @ResponseBody
    @RequestMapping(value = "/delete")
    public Response delete(Long id){

        labelService.delete(id);

        return Response.ok();
    }

}
